package edu.dgut.bbssystem.controller;

import edu.dgut.bbssystem.common.ActionResult;
import edu.dgut.bbssystem.common.Constant;
import edu.dgut.bbssystem.common.PageInfo;
import edu.dgut.bbssystem.pojo.User;
import edu.dgut.bbssystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public ActionResult register(@RequestBody User user){
        ActionResult actionResult = new ActionResult();
        int add = userService.add(user);
        actionResult.setCode(add);
        return actionResult;
    }
    @GetMapping("/existUserName")
    public ActionResult existUserName(String userName){
        ActionResult actionResult = new ActionResult();
        boolean b = userService.existUserName(userName);
        int code = b? Constant.USERNAME_EXIST:Constant.USERNAME_NOT_EXIST;
        actionResult.setCode(code);
        return actionResult;
    }
    @PostMapping("/login")
    public ActionResult login(HttpSession session,@RequestBody User user){
        ActionResult actionResult = new ActionResult();
        int code = userService.login(user, session);
        actionResult.setCode(code);
        return actionResult;
    }
    @GetMapping("/getById")
    public ActionResult getById(Integer userId){
        ActionResult actionResult = new ActionResult();
        User user = userService.getById(userId);
        int code = user == null? Constant.FAIL: Constant.SUCCESS;
        actionResult.setCode(code);
        actionResult.setData(user);
        return actionResult;
    }

    @PutMapping("/edit")
    public ActionResult ss(@RequestBody User user){
        ActionResult actionResult = new ActionResult();
        int code = Constant.FAIL;
        int result = userService.update(user);
        code = result==1?Constant.SUCCESS:Constant.FAIL;
        actionResult.setCode(code);
        return actionResult;
    }
    @GetMapping("/loginInfo")
    public ActionResult loginInfo(HttpSession session){
        ActionResult actionResult = new ActionResult();
        User user = (User) session.getAttribute("user");
        int code = user == null? Constant.FAIL: Constant.SUCCESS;
        actionResult.setCode(code);
        actionResult.setData(user);
        return actionResult;
    }

    @GetMapping("/userPage")
    public ActionResult userPage(int pageNum, int pageSize){
        ActionResult actionResult = new ActionResult();
        PageInfo<User> userPageInfo =  userService.pageQueryAll(pageNum, pageSize);
        int code = Constant.SUCCESS;
        actionResult.setCode(code);
        actionResult.setData(userPageInfo);
        return actionResult;
    }

    @DeleteMapping("user")
    public ActionResult deleteUsers(@RequestBody int[] userIds){
        ActionResult actionResult = new ActionResult();
        int code =  userService.deleteUsersByIds(userIds);
        actionResult.setCode(code);
        return actionResult;
    }

    @DeleteMapping("/logout")
    public ActionResult logout(HttpSession session){
        ActionResult actionResult = new ActionResult();

        int code = userService.logout(session);
        actionResult.setCode(code);

        return actionResult;
    }

    @GetMapping("/getUserId")
    public  ActionResult getUserId(HttpSession session){
        ActionResult actionResult = new ActionResult();
        int code = Constant.FAIL;
        int userId = userService.getUserId(session);
        if (userId != 0){
            actionResult.setData(userId);
            code = Constant.SUCCESS;
        }
        actionResult.setCode(code);
        return actionResult;
    }
}
